// // Modules dependencies
var gulp = require('gulp')
var exec = require('child_process').exec;
var sass = require('gulp-sass')
var sourcemaps = require('gulp-sourcemaps')
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
var nodemon = require('gulp-nodemon')
var htmlmin = require('gulp-htmlmin');
var imagemin = require('gulp-imagemin');
var cssnano = require('gulp-cssnano')
var uglify = require('gulp-uglify')
var jshint = require('gulp-jshint');
var rename = require('gulp-rename');
var gulpSequence = require('gulp-sequence');
var path = require('path')
var del = require('del');
var revReplace = require('gulp-rev-replace');
//修改后的gulp-rev
//var rev = require('./gulp-rev.js');
var rev = require('gulp-rev');
// autoprefixer
var autoprefixer = require('gulp-autoprefixer');

// 删除文件
gulp.task('clean', function () {
  del([
    'app/public/dist/css/**/*',
    'app/public/dist/js/**/*',
    'app/public/dist/img/**/*',
    'app/public/dist/module/*',
    'app/public/dist/static/*',
    'app/public/dist/views/**/*',
    'app/public/dist/*.html'
  ])
});
// 压缩ejs
// gulp.task('ejs', function () {
//     return gulp.src('views/**/*.ejs')
//         .pipe(htmlmin({ collapseWhitespace: true }))
//         .pipe(gulp.dest('dist/views/'))
// });

// 压缩scss
gulp.task('scss', function () {
  return gulp.src('app/public/pro/scss/**/*.scss')
    .pipe(sass())
    .on('error', sass.logError)
    .pipe(autoprefixer({
      browsers: ['last 20 versions', 'Android >= 4.0'],
      cascade: false //是否美化属性值 默认：true 像这样：
    }))
    .pipe(gulp.dest('app/public/pro/css/'));
});
//copy css
gulp.task('copycss', function () {
  return gulp.src(['app/public/pro/css/**/*.css'])
    .pipe(gulp.dest('app/public/temp/css/'))
    .pipe(reload({
      stream: true
    }));
})
//copy js
gulp.task('copyjs', function () {
  return gulp.src(['app/public/pro/js/**/*.js'])
    .pipe(gulp.dest('app/public/temp/js/'))
  // .pipe(reload({
  //   stream: true
  // }));
  //js变化不通知浏览器，手动刷新处理
})

//copy img
gulp.task('copyimg', function () {
  return gulp.src(['app/public/pro/img/**/*'])
    .pipe(gulp.dest('app/public/temp/img/'))
  /* .pipe(reload({
    stream: true
  })); */
})
//最小化全部css
gulp.task('cssmin', ['copycss'], function () {
  return gulp.src('app/public/temp/**/*.css')
    .pipe(cssnano())
    .pipe(rev())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('app/public/dist'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('app/public/rev/css'));
})

// 压缩js
gulp.task('jsmin', ['copyjs'], function () {
  return gulp.src(['app/public/temp/**/*.js'])
    .pipe(sourcemaps.init())
    .pipe(jshint())
    .pipe(jshint.reporter('default'))
    .pipe(rev())
    .pipe(uglify())
    .pipe(rename({
      suffix: '.min'
      }))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('app/public/dist'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('app/public/rev/js'));
});

gulp.task('copycssbg', function () {
  return gulp.src('app/public/temp/img/css/*')
    .pipe(gulp.dest('app/public/dist/img/css/'))
});

// 压缩img
gulp.task('imgmin', ['copyimg'], function () {
  return gulp.src(['app/public/temp/**/*.jpg', 'app/public/temp/**/*.png', '!app/public/temp/img/css/*']) //引入所有需处理的Img
    /* .pipe(imagemin({
      optimizationLevel: 3,
      progressive: true,
      interlaced: true
    })) //压缩图片 */
    // 如果想对变动过的文件进行压缩，则使用下面一句代码
    // .pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true }))) 
    .pipe(rev())
    .pipe(gulp.dest('app/public/dist'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('app/public/rev/img'))
  // .pipe(notify({ message: '图片处理完成' }));
});

//Html替换css、js文件版本
gulp.task("revHtml", function () {
  var manifest = gulp.src("app/public/rev/**/*.json");
  return gulp.src('app/view/**/*.html')
    .pipe(revReplace({
      manifest: manifest,
       prefix: "https://sfile.tl100.com/tianli_cartoon_website" //是否使用cdn前缀
      //prefix: ""
    }))
    .pipe(gulp.dest('app/public/dist/views'));
});


//开起浏览器同步
gulp.task('browserSync', function () {
  browserSync.init(null, {
    proxy: "localhost:7005",
    files: ["app/public/temp/css/*.*", "app/public/temp/js/*.*", "app/public/temp/js/**/*.*", "app/public/temp/img/*.*"],
    // browser: "google chrome",
    notify: false,
    port: 3008
  });
  // 监听所有css文档
  gulp.watch('app/public/pro/scss/**/*.scss', ['scss']);
  //监控其他静态资源变化
  gulp.watch(['app/public/pro/css/*'], ['copycss']);
  gulp.watch(['app/public/pro/js/**/*.js'], ['copyjs']);
  gulp.watch(['app/public/pro/img/*'], ['copyimg']);
  // 监听ejs
  gulp.watch('app/view/**/*.html', reload);
});

gulp.task('build', ['clean'], function (cb) {
  gulpSequence('scss', ['cssmin', 'jsmin', 'imgmin', 'copycssbg'], 'revHtml', cb);
});
gulp.task('dev', function (cb) {
  gulpSequence('scss', ['copycss', 'copyjs', 'copyimg'], 'browserSync', cb);
});